const { con, Result } = require('./common');
//举报人 举报人（商家款） 被举报的人 被举报的商家 被举报的商品 被举报的评论 被举报的动态 举报描述
let Report = async (user_id, business_id, user_id2, business_id2, commodity_id, comment_id, dynamic_id, report_context) => {
        let res = new Result();
        let sql2 = `select * from report`;
        let [rows2] = await con.execute(sql2);
        if (user_id) {
                if (user_id2) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,userr_id,userr2_id,report_context)values(1,${user_id},${user_id2},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,userr_id,userr2_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${user_id},${user_id2},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
                if (business_id2) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,userr_id,businessr2_id,report_context)values(1,${user_id},${business_id2},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,userr_id,businessr2_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${user_id},${business_id2},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
                if (commodity_id) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,userr_id,commodityr_id,report_context)values(1,${user_id},${commodity_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,userr_id,commodityr_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${user_id},${commodity_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
                if (comment_id) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,userr_id,commentr_id,report_context)values(1,${user_id},${comment_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,userr_id,commentr_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${user_id},${comment_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
                if (dynamic_id) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,userr_id,dynamicr_id,report_context)values(1,${user_id},${dynamic_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,userr_id,dynamicr_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${user_id},${dynamic_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
        }
        if (business_id) {
                if (user_id2) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,businessr_id,userr2_id,report_context)values(1,${business_id},${user_id2},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,businessr_id,userr2_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${business_id},${user_id2},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
                if (business_id2) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,businessr_id,businessr2_id,report_context)values(1,${business_id},${business_id2},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,businessr_id,businessr2_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${business_id},${business_id2},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
                if (commodity_id) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,businessr_id,commodityr_id,report_context)values(1,${business_id},${commodity_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,businessr_id,commodityr_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${business_id},${commodity_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
                if (comment_id) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,businessr_id,commentr_id,report_context)values(1,${business_id},${comment_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,businessr_id,commentr_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${business_id},${comment_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
                if (dynamic_id) {
                        if (rows2.length == 0) {
                                let sql3 = `insert into report(report_id,businessr_id,dynamicr_id,report_context)values(1,${business_id},${dynamic_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        } else {
                                let sql3 = `insert into report(report_id,businessr_id,dynamicr_id,report_context)values(${rows2[rows2.length - 1].report_id + 1},${business_id},${dynamic_id},'${report_context}')`;
                                let [rows3] = await con.execute(sql3);
                        }
                }
        }
        return res;
}
let Report_look = async () => {
        let res = new Result();
        let sql = `select * from report`;
        let arr1 = [];
        let [rows] = await con.execute(sql);
        for (let i = 0; i < rows.length; i++) {
                if (rows[i].userr_id) {
                        let sql1 = `select * from user where user_id = ${rows[i].userr_id}`;
                        let [rows1] = await con.execute(sql1);
                        arr1[i] = rows1[0];
                }
                if (rows[i].businessr_id) {
                        let sql1 = `select * from business where business_id = ${rows[i].businessr_id}`;
                        let [rows1] = await con.execute(sql1);
                        arr1[i] = rows1[0];
                }
                if (rows[i].commodityr_id) {
                        let sql1 = `select * from commodity where commodity_id = ${rows[i].commodityr_id}`;
                        let [rows1] = await con.execute(sql1);
                        arr1[i].report = rows1[0];
                        arr1[i].report2 = 1
                }
                if (rows[i].dynamicr_id) {
                        let sql1 = `select * from dynamic where dynamic_id = ${rows[i].dynamicr_id}`;
                        let [rows1] = await con.execute(sql1);
                        arr1[i].report = rows1[0];
                        arr1[i].report2 = 2;
                }
                if (rows[i].userr2_id) {
                        let sql1 = `select * from user where user_id = ${rows[i].userr2_id}`;
                        let [rows1] = await con.execute(sql1);
                        arr1[i].report = rows1[0];
                        arr1[i].report2 = 3;
                }
                if (rows[i].businessr2_id) {
                        let sql1 = `select * from business where business_id = ${rows[i].businessr2_id}`;
                        let [rows1] = await con.execute(sql1);
                        arr1[i].report = rows1[0];
                        arr1[i].report2 = 4;
                }
                if (rows[i].commentr_id) {
                        let sql1 = `select * from comment where comment_id = ${rows[i].commentr_id}`;
                        let [rows1] = await con.execute(sql1);
                        arr1[i].report = rows1[0];
                        arr1[i].report2 = 5;
                }
                arr1[i].report_context = rows[i].report_context;
                arr1[i].report_id = rows[i].report_id;
        }
        res.data = { arr1 };
        return res;
}
let Report_delete = async (report_id) => {
        let res = new Result();
        let sql = `delete from report where report_id = ${report_id}`;
        let [rows] = await con.execute(sql);
        return res;
}
module.exports = { Report, Report_look, Report_delete };








